﻿using UnityEngine;
using System.Collections;

public class ZoneCamera : MonoBehaviour
{
    public Camera _GroudCamera;

    public Camera _WorldCamera;

    public Camera _BlockCamera;

    public float _OrthSize = 0;

    public Rect _RectViewLimit;

    public Vector2 _OtrhSizeLimit;

    float _ZoneWidth = 1024;

    float _ZoneHeight = 768;

	void Start ()
    {
        //_OrthSize = _OtrhSizeLimit.y;

        ResizeOrthSize(0);

        Move(new Vector2(2323, 1236));
    }

    public void ResizeArea(float Width, float Height)
    {
        _ZoneWidth = Width;
        _ZoneHeight = Height;

        UpdateOrthSize(_OrthSize);

        UpdateSize();
    }

    public void ResizeOrthSize(float delta)
    {
        _OrthSize += delta;
        _OrthSize = Mathf.Clamp(_OrthSize, _OtrhSizeLimit.x, _OtrhSizeLimit.y);

        UpdateOrthSize(_OrthSize);
        UpdateSize();
    }

    void UpdateOrthSize(float otrhSize)
    { 
        _GroudCamera.orthographicSize = otrhSize;
        _WorldCamera.orthographicSize = otrhSize;
        _BlockCamera.orthographicSize = otrhSize;
    }

    void UpdateSize()
    {
        float factor = (float)Screen.width / (float)Screen.height;

        _RectViewLimit.max = new Vector2(_ZoneWidth - _OrthSize * factor, _ZoneHeight - _OrthSize );
        _RectViewLimit.min = new Vector2( _OrthSize * factor, _OrthSize );
    }

    public void Move(Vector2 delta)
    {
        Vector3 pos = _WorldCamera.transform.position;
        pos.x += delta.x;
        pos.y += delta.y;

        if (pos.x > _RectViewLimit.max.x) pos.x = _RectViewLimit.max.x;
        if (pos.y > _RectViewLimit.max.y) pos.y = _RectViewLimit.max.y;

        if (pos.x < _RectViewLimit.min.x) pos.x = _RectViewLimit.min.x;
        if (pos.y < _RectViewLimit.min.y) pos.y = _RectViewLimit.min.y;

        _WorldCamera.transform.position = pos;
        _GroudCamera.transform.position = pos;
        _BlockCamera.transform.position = pos;

    }

    private void Update()
    {
        float x = Input.GetAxis("Mouse ScrollWheel");
        ResizeOrthSize(x * 100);
    }
}
